
' 生成一个 Label 控件
' 如果 name 已经被占用，将会抛出异常
'
' @param ctrs 用于创建控件的控制器集合
' @param name 控件的名字
' @param height 控件的高度
' @param width 控件的宽度
' @param left 与控件左边界的距离
' @param top 与控件上边界的距离
' @optional-param caption 控件内显示的文字内容
' @optional-param fontSize 文字大小
' @return Control Label 控件对象
Public Function createLabel(ByRef ctrs As Controls, ByRef name As String, _
    ByRef height As Double, ByRef width As Double, _
    ByRef left As Double, ByRef top As Double, _
    Optional ByRef caption As String = "", Optional ByRef fontSize As Double = -1) As Control

    Set createLabel = ctrs.Add("Forms.Label.1")
    With createLabel
        .Name = name
        .Height = height
        .Width = width
        .Left = left
        .Top = top

        If isError(caption) = False Then
            .Caption = caption
        End If

        If fontSize > 0 Then
            .Object.Font.Size = fontSize
        End If
    End With
End Function

' 生成一个 TextBox 控件
' 如果 name 已经被占用，将会抛出异常
'
' @param ctrs 用于创建控件的控制器集合
' @param name 控件的名字
' @param height 控件的高度
' @param width 控件的宽度
' @param left 与控件左边界的距离
' @param top 与控件上边界的距离
' @optional-param fontSize 文字大小
' @optional-param text 设置 `TextBox` 的文本框内的内容
' @optional-param value 设置默认值。即没有设置 text 时的值
' @optional-param tabIndex 设置 tab index
' @optional-param scrollBars 设置滚动条的位置
' @optional-param multiLine 设置是否多行显示
' @return Control TextBox 控件对象
Public Function createTextBox(ByRef ctrs As Controls, ByRef name As String, _
    ByRef height As Double, ByRef width As Double, _
    ByRef left As Double, ByRef top As Double, _
    Optional ByRef fontSize As Double = -1, _
    Optional ByRef text As Variant = "", Optional ByRef value As Variant = "", _
    Optional ByRef tabIndex As Integer = 0, _
    Optional ByRef scrollBars As Integer = 0, _
    Optional ByRef multiLine As Boolean = False) As MsForms.TextBox

    Set createTextBox = ctrs.Add("Forms.TextBox.1")
    With createTextBox
        .Name = name
        .Height = height
        .Width = width
        .Left = left
        .Top = top

        If tabIndex > 0 Then
            .TabIndex = tabIndex
        End If

        With .Object
            If fontSize > 0 Then
                .Font.Size = fontSize
            End If

            If value <> "" Then
                .Value = value
            End If

            If text <> "" Then
                .Text = text
            End If
        End With

        If scrollBars > 0 And scrollBars <= 3 Then
            .ScrollBars = scrollBars
        End If

        If multiLine Then
            .MultiLine = True
        End If
    End With
End Function

' 生成一个 CommandButton 控件
' 如果 name 已经被占用，将会抛出异常
'
' @param ctrs 用于创建控件的控制器集合
' @param name 控件的名字
' @param height 控件的高度
' @param width 控件的宽度
' @param left 与控件左边界的距离
' @param top 与控件上边界的距离
' @optional-param caption 控件内显示的文字内容
' @optional-param fontSize 文字大小
' @optional-param tabIndex 设置 tab index
' @return Control TextBox 控件对象
Public Function createButton(ByRef ctrs As Controls, ByRef name As String, _
    ByRef height As Double, ByRef width As Double, _
    ByRef left As Double, ByRef top As Double, _
    Optional ByRef caption As String, Optional ByRef fontSize As Double = -1, _
    Optional ByRef tabIndex As Integer = 0) As Control

    Set createButton = ctrs.Add("Forms.CommandButton.1")
    With createButton
        .Name = name
        .Height = height
        .Width = width
        .Left = left
        .Top = top

        If isError(caption) = False Then
            .Caption = caption
        End If

        If tabIndex > 0 Then
            .TabIndex = tabIndex
        End If

        With .Object
            If fontSize > 0 Then
                .Font.Size = fontSize
            End If
        End With
    End With
End Function

' 生成一个 RefEdit 控件
' 如果 name 已经被占用，将会抛出异常
'
' @param ctrs 用于创建控件的控制器集合
' @param name 控件的名字
' @param height 控件的高度
' @param width 控件的宽度
' @param left 与控件左边界的距离
' @param top 与控件上边界的距离
' @optional-param fontSize 文字大小
' @optional-param text 设置 `TextBox` 的文本框内的内容
' @optional-param value 设置默认值。即没有设置 text 时的值
' @optional-param tabIndex 设置 tab index
' @return Control RefEdit 控件对象
Public Function createRefEdit(ByRef ctrs As Controls, ByRef name As String, _
    ByRef height As Double, ByRef width As Double, _
    ByRef left As Double, ByRef top As Double, _
    Optional ByRef fontSize As Double = -1, _
    Optional ByRef text As Variant = "", Optional ByRef value As Variant = "", _
    Optional ByRef tabIndex As Integer = 0) As Control

    ' Set createRefEdit = ctrs.Add("Forms.RefEdit.1")
    Set createRefEdit = ctrs.Add("RefEdit.Ctrl")
    With createRefEdit
        .Name = name
        .Height = height
        .Width = width
        .Left = left
        .Top = top

        If tabIndex > 0 Then
            .TabIndex = tabIndex
        End If

        With .Object
            If fontSize > 0 Then
                .Font.Size = fontSize
            End If

            If value <> "" Then
                .Value = value
            End If

            If text <> "" Then
                .Text = text
            End If
        End With
    End With
End Function

' 生成一个 TextBox 控件
' 如果 name 已经被占用，将会抛出异常
'
' @param ctrs 用于创建控件的控制器集合
' @param name 控件的名字
' @param height 控件的高度
' @param width 控件的宽度
' @param left 与控件左边界的距离
' @param top 与控件上边界的距离
' @optional-param caption 控件内显示的文字内容
' @optional-param value 设置默认值: 是否选中
' @optional-param fontSize 文字大小
' @optional-param tabIndex 设置 tab index
' @return Control CheckBox 控件对象
Public Function createCheckBox(ByRef ctrs As Controls, ByRef name As String, _
    ByRef height As Double, ByRef width As Double, _
    ByRef left As Double, ByRef top As Double, _
    Optional ByRef caption As String, Optional ByRef value As Boolean = False, _
    Optional ByRef fontSize As Double = -1, _
    Optional ByRef tabIndex As Integer = 0) As Control

    Set createCheckBox = ctrs.Add("Forms.CheckBox.1")
    With createCheckBox
        .Name = name
        .Height = height
        .Width = width
        .Left = left
        .Top = top

        .Value = value

        If isError(caption) = False Then
            .Caption = caption
        End If

        If tabIndex > 0 Then
            .TabIndex = tabIndex
        End If

        With .Object
            If fontSize > 0 Then
                .Font.Size = fontSize
            End If
        End With
    End With
End Function

' 生成一个 OptionButton 控件
' 如果 name 已经被占用，将会抛出异常
'
' @param ctrs 用于创建控件的控制器集合
' @param name 控件的名字
' @param height 控件的高度
' @param width 控件的宽度
' @param left 与控件左边界的距离
' @param top 与控件上边界的距离
' @optional-param caption 控件内显示的文字内容
' @optional-param value 设置默认值: 是否选中
' @optional-param fontSize 文字大小
' @optional-param tabIndex 设置 tab index
' @optional-param groupName 设置当前控件所属的分组名称
' @return Control OptionButton 控件对象
Public Function createOptionButton(ByRef ctrs As Controls, ByRef name As String, _
    ByRef height As Double, ByRef width As Double, _
    ByRef left As Double, ByRef top As Double, _
    Optional ByRef caption As String, Optional ByRef value As Boolean = False, _
    Optional ByRef fontSize As Double = -1, _
    Optional ByRef tabIndex As Integer = 0, _
    Optional ByRef groupName As String) As MsForms.OptionButton

    Set createOptionButton = ctrs.Add("Forms.OptionButton.1")
    With createOptionButton
        .Name = name
        .Height = height
        .Width = width
        .Left = left
        .Top = top

        .Value = value

        If isError(caption) = False Then
            .Caption = caption
        End If

        If isError(groupName) = False Then
            .groupName = groupName
        End If

        If tabIndex > 0 Then
            .TabIndex = tabIndex
        End If

        With .Object
            If fontSize > 0 Then
                .Font.Size = fontSize
            End If
        End With
    End With
End Function

' 生成一个 Frame 控件
' 如果 name 已经被占用，将会抛出异常
'
' @param ctrs 用于创建控件的控制器集合
' @param name 控件的名字
' @param height 控件的高度
' @param width 控件的宽度
' @param left 与控件左边界的距离
' @param top 与控件上边界的距离
' @optional-param caption 控件内显示的文字内容
' @optional-param fontSize 控件文字大小
' @return Control Frame 控件对象
Public Function createFrame(ByRef ctrs As Controls, ByRef name As String, _
    ByRef height As Double, ByRef width As Double, _
    ByRef left As Double, ByRef top As Double, _
    Optional ByRef caption As String, Optional ByRef fontSize As Double = -1) As MsForms.Frame

    Set createFrame = ctrs.Add("Forms.Frame.1")
    With createFrame
        .Name = name
        .Height = height
        .Width = width
        .Left = left
        .Top = top

        If isError(caption) = False Then
            .Caption = caption
        End If

        With .Object
            If fontSize > 0 Then
                .Font.Size = fontSize
            End If
        End With
    End With
End Function